package com.patents.controller;


import com.patents.dao.UsersMapper;
import com.patents.service.UsersService;
import com.patents.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@CrossOrigin
public class UsersController {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
   /* UsersService usersService;*/
    @Qualifier("UsersServiceImpl")
    public UsersService usersService;

    @RequestMapping("/login")
    public String login(@RequestBody Users users, HttpServletRequest request) {
        String flag = "error";
        System.out.println("users:" + users);
        Users us = usersMapper.getUserByMassage(users.getUsername(), users.getPassword());
        System.out.println("users:" + us);
        HashMap<String, Object> res = new HashMap<>();
        if (us != null) {
            flag = "ok";
        }
        res.put("flag",flag);
        res.put("user",users);
        String res_json = JSON.toJSONString(res);
        return res_json;
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestBody Users users) {
        System.out.println("user:*****" + users);
        usersService.addUsers(users);
        return "ok";
    }
}
